<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/13
 * Time: 14:47
 */

namespace App\Model;
use Illuminate\Database\Eloquent\Model;

class Address extends Model {
	/**
	 * 用户收货地址
	 * @var string
	 */
	protected $table = 'address';

	protected $fillable = [
		'id',
		'uid',
		'phone',
		'name',
		'province',
		'city',
		'district',
		'address',
		'landline',
		'zip_code',
		'default',
	];

	/**
	 * 统计收货地址数量
	 * @param null $uid
	 * @return int
	 */
	public function count_address($uid = null) {
		return $this->where(['uid' => $uid])->count();
	}

	/**
	 * 获取默认地址
	 * @param null $uid
	 * @return bool|Model|null|static
	 */
	public function get_address($uid = null) {
		$rico = $this->where(['uid' => $uid, 'default' => 1])->first();
		if ($rico) {
			return $rico;
		} else {
			return false;
		}
	}


    /**
     * 获取默认地址
     * @param null $uid
     * @return bool|Model|null|static
     */
    public function get_id_address($id) {
        $rico = $this->where(['id' => $id])->first();
        if ($rico) {
            return $rico;
        } else {
            return false;
        }
    }
}